<?php
require_once ( get_template_directory() . '/config.php' );

if ( get_magic_quotes_gpc() ) {
	$_POST = array_map( 'stripslashes_deep', $_POST );
	$_GET = array_map( 'stripslashes_deep', $_GET );
	$_COOKIE = array_map( 'stripslashes_deep', $_COOKIE );
	$_REQUEST = array_map( 'stripslashes_deep', $_REQUEST );
} else {
	$_POST = array_map( 'stripslashes_deep', $_POST );
	$_GET = array_map( 'stripslashes_deep', $_GET );
	$_COOKIE = array_map( 'stripslashes_deep', $_COOKIE );
	$_REQUEST = array_map( 'stripslashes_deep', $_REQUEST );
}

add_action('after_setup_theme', 'my_theme_setup');
function my_theme_setup(){
    load_theme_textdomain(CSS_DOMAIN, get_template_directory() . '/languages');    
}


add_action('init', 'css_init');
function css_init() {	
	add_post_type_support( 'page', 'excerpt' );
	add_theme_support( 'post-thumbnails' ); 
}



// per far funzionare ajaxurl
add_action('wp_head','pluginname_ajaxurl');
function pluginname_ajaxurl() {
?>
<script type="text/javascript">
	var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}


// Left Sidebar
if ( function_exists('register_sidebar') ){
	register_sidebar( array(
		'name' => __( 'Left Sidebar', CSS_DOMAIN ),
		'id' => 'left-sidebar',
		'description' => __( 'Left Sidebar widget area', CSS_DOMAIN ),
		'before_widget' => '<div id="%1$s" class="box-title %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h2 class="title title-grafico">',
		'after_title' => '</h2>',
	) );
}



if ( basename($_SERVER['PHP_SELF']) != 'wp-login.php' ) {

	add_action('init', 'css_init_styles');
	function css_init_styles() {		
		wp_register_style('jquery_mobile_1_4_0_min_css', get_bloginfo('template_url').'/css/themes/default/jquery.mobile-1.4.0.min.css',__FILE__);
		wp_enqueue_style('jquery_mobile_1_4_0_min_css');
		
		wp_register_style('jqm_demos_css', get_bloginfo('template_url').'/_assets/css/jqm-demos.css',__FILE__);
		wp_enqueue_style('jqm_demos_css');
		
		wp_register_style('jquery_mobile_datepicker_css', 'https://rawgithub.com/arschmitz/jquery-mobile-datepicker-wrapper/v0.1.1/jquery.mobile.datepicker.css',__FILE__);
		wp_enqueue_style('jquery_mobile_datepicker_css');
	
		//wp_register_style('jquery_ui_css', 'http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css',__FILE__);
		//wp_enqueue_style('jquery_ui_css');
		
	}
	
	
	if ( basename($_SERVER['PHP_SELF']) == 'wp-login.php' ) {
    	add_action( 'admin_init','css_admin_init_styles');
	}
	
	add_action( 'admin_init', 'css_admin_init_styles');
	function css_admin_init_styles() {		
		wp_deregister_style('jqm_demos_css');	
		wp_deregister_style('jquery_mobile_1_4_0_min_css');
	}
	
	
	add_action( 'init','css_init_js');
	function css_init_js() {
		
		//wp_enqueue_script( 'jquery', '../wp-includes/js/jquery/jquery.js', false, '1.8.3');	
		
		wp_register_script('jquery_js', get_bloginfo('template_url').'/js/jquery.js',__FILE__);	
		wp_enqueue_script('jquery_js');	
		
		wp_register_script('index_js', get_bloginfo('template_url').'/_assets/js/index.js',__FILE__);	
		wp_enqueue_script('index_js');
	
		wp_register_script('jquery_mobile_1_4_0_min_js', get_bloginfo('template_url').'/js/jquery.mobile-1.4.0.min.js',__FILE__);	
		wp_enqueue_script('jquery_mobile_1_4_0_min_js');
		
		wp_register_script('jquery_ui_datepicker_js', 'https://rawgithub.com/jquery/jquery-ui/1.10.4/ui/jquery.ui.datepicker.js',__FILE__);	
		wp_enqueue_script('jquery_ui_datepicker_js');
		
		wp_register_script('jquery_mobile_datepicker_js', 'https://rawgithub.com/arschmitz/jquery-mobile-datepicker-wrapper/v0.1.1/jquery.mobile.datepicker.js',__FILE__);	
		wp_enqueue_script('jquery_mobile_datepicker_js');
		
	}
 
	
	
	
	
	
	add_action( 'admin_init','css_admin_init_js');
	function css_admin_init_js() {	
		wp_deregister_script('jquery_js');
		wp_deregister_script('index_js');	
		wp_deregister_script('jquery_mobile_1_4_0_min_js');
	}

}



add_action( 'init', 'css_register_menus' );
function css_register_menus() {
	register_nav_menus(
		array(       	
			"primary_menu" => "Primary Menu"
		)
	);
}




add_action("admin_menu", "polselli_plugin_add_menu");	
function polselli_plugin_add_menu() {		
	$ico_url = '../wp-content/themes/css/images/css_ico.png';
	add_menu_page(__( 'CSS Admin Page', CSS_DOMAIN ), __( 'CSS Admin Page', CSS_DOMAIN ), 'manage_options', 'main_page', 'css_func_main_page', $ico_url);
	
	add_submenu_page("main_page", __( 'CSS custom fields page', CSS_DOMAIN ), __( 'CSS custom fields page', CSS_DOMAIN ), 'manage_options', "cpf_custom_fields_page", 'css_custom_fields_func');		
	
	//remove_submenu_page('main_page','main_page');
	
}


function css_wp_get_current_user() {
	// Insert pluggable.php before calling get_currentuserinfo()
	require (ABSPATH . WPINC . '/pluggable.php');
	global $current_user;
	get_currentuserinfo();
	return $current_user;
}

function no_permissions() {	
	$msg = "<p>" . __( 'Sorry, you have not permissions for this page', CSS_DOMAIN ) . "</p>";	
	return $msg;
}


function get_remote_url($rem_url, $rem_port, $api, $iduser) {	
	
	
	switch ($api) {
	    case GetAccServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;
	    case WelcomeServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;
	    case PianiServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;
	    case GetEffettuatiServletOperatori:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;    
	    case DeleteEffettuatiServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;    
	    case GetEffettuatiServletOperatori:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;       
		case EffettuatiServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;  
		case ProgrammiDettaglioServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;
		case EffettuatiAccServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;
	    case AssistitiAnaServlet:
	        return $rem_url.':'.$rem_port.'/webservice/'.$api.'?idoperatore='.$iduser;
	        break;    
	        
	        
	        
	        
	}	
}



add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
	global $rem_url, $rem_port;
	$req = $_REQUEST;	
	
	$name = $_REQUEST['first_name'];
	$surname = $_REQUEST['last_name'];
	$username = $_REQUEST['user_login'];
	$password = $_REQUEST['pass1'];
		
	$url = $rem_url.':'.$rem_port.'/webservice/saveOp?Cognome='.$surname.'&Nome='.$name.'&codopt='.$username.'&password='.$password;
	
	$remote = get_user_meta($user_id, 'remote', true); 
	
	if ($remote != 1) {
		$res = getContent ($url);		
	}		
}






function getContent ($url) { 
    // Crea la risorsa CURL
    $ch = curl_init();
 
    // Imposta l'URL e altre opzioni
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    // Scarica l'URL e lo passa al browser
    $output = curl_exec($ch);
    $info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // Chiude la risorsa curl
    curl_close($ch);
    if ($output === false || $info != 200) {
		$output = null;
    }
    return $output; 
}



add_action('wp_login', 'redir_login');
function redir_login() { 
	
	$blogurl = get_bloginfo( 'wpurl' );	
	
	wp_redirect( home_url() );
  	exit;
	
}



?>